﻿using AgencyManagerSystem.Common;
using AgencyManagerSystem.Model;
using Dapper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AgencyManagerSystem.DataFetcher
{
    public class RetailFetcher:ErpFetcher<RetailModel>
    {
        public RetailFetcher() : base()
        {

        }
        public RetailModel GetByCode(string code)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Code", Op = "=", Value = code } };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            else
                return null;
        }
        public override void AddDefaultFilter(StringBuilder stringBuilder, DynamicParameters dynamicParameters)
        {
            if (userProfile == null)
                return;
            if (userProfile.Type == UserType.Department)
            {
                string keyStr = String.Format("@{0}_{1}", "Department", COUNTER++);
                stringBuilder.AppendFormat(" AND (CreateDepartment = {0} OR ModifyDepartment = {0} OR ApproveDepartment = {0})", keyStr);
                dynamicParameters.Add(keyStr, userProfile.Department);
            }
            if (userProfile.Type == UserType.Person)
            {
                string keyStr = String.Format("@{0}_{1}", "Persion", COUNTER++);
                stringBuilder.AppendFormat(" AND (Creator = {0} OR Modifier = {0} OR Approver = {0})", keyStr);
                dynamicParameters.Add(keyStr, userProfile.UserId);
            }
            if (userProfile.Type == UserType.SiteOrSupplier)
            {
                string keyStr = String.Format("@{0}_{1}", "AutoId", COUNTER++);
                stringBuilder.AppendFormat(" AND (AutoId = {0})", keyStr);
                dynamicParameters.Add(keyStr, userProfile.Site);
            }
        }
        public IList<RetailModel> ListBySales(int sales)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Sales", Op = "=", Value = sales } };
            var list = List(conditions);
            return list;
        }
        public IList<RetailModel> ListByDepartment(int sales)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Department", Op = "=", Value = sales } };
            var list = List(conditions);
            return list;
        }
    }
}
